#include "initsock.h"

void initsock(int& lfd)
{
    lfd = socket(AF_INET, SOCK_STREAM, 0);
    if(lfd == -1)
    {
        perror("socket---------------------");
        return ;
    }

    //设置端口复用
    int reuse = 1;
    setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));

    //绑定IP和端口
    struct sockaddr_in saddr;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(9999);
    saddr.sin_addr.s_addr = INADDR_ANY;
    int ret = bind(lfd, (struct sockaddr*)&saddr, sizeof(saddr));
    if(ret == -1)
    {
        perror("bind---------------------");
        return ;
    }

    //监听
    ret = listen(lfd, 128);
    if(ret == -1)
    {
        perror("listen---------------------");
        return ;
    }
}